package cn.tedu.admin.mall.controller;

import cn.tedu.admin.mall.pojo.bo.CategoryBO;
import cn.tedu.admin.mall.pojo.param.CategoryAddParam;
import cn.tedu.admin.mall.pojo.param.CategoryUpdateParam;
import cn.tedu.admin.mall.pojo.vo.CategoryListItemVO;
import cn.tedu.admin.mall.service.CategoryService;
import cn.tedu.tmall.common.vo.PageData;
import cn.tedu.tmall.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

//url 可以加版本号
//app 1.0   categories/list
//app 1.1  categories/list(arg)
//app 5.0  categories/list(arg,arg)
//app 强升
@Api(tags = "类别管理")
//@Valid 提醒注意引入正确的包
@Validated
//todo 1 验证框架不生效
//
@Slf4j
@RequestMapping("/categories")
@RestController
public class CategoryController {

    //http://tea.mall/v1/category/list
    //http://tea.mall/category/add
    //http://tea.mall/category/info
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("类别列表")
    @ApiOperationSupport(order = 200)
    @GetMapping("/category/list/{pageNum}/{pageSize}")
    public JsonResult listCategory(
            @PathVariable
            @Range(min = 1,message = "请输入有效的pageNum")
                    Integer pageNum,
            @PathVariable
            @Range(min = 1,max = 50,message = "请输入有效的pageSize")
                    Integer pageSize){
        //1.pageSize 要做长度限制 假定20,如果不做限制,客户端传了一个很大的值50000,内存溢出
        //2.pageSize 可以不传,我们搞个默认值
        //pageNum 需要校验吗? 大于0 小于最大页数
        //小于最大页数 太复杂,因为你传的再大活着再不合理,数据库不返回数据就行了
        log.debug("入参:pageNum:{},pageSize:{}",pageNum,pageSize);
        pageSize = pageSize == null ? 50 : pageSize;
        //BO转VO
        PageData<CategoryBO> categoryBOPageData = categoryService.listCategory(pageNum,pageSize);
        PageData<CategoryListItemVO> result = new PageData<>();
        BeanUtils.copyProperties(categoryBOPageData,result);
        PageData<CategoryListItemVO> categoryListItemVOPageData = getCategoryBOPageData(categoryBOPageData);
        //return contentService.listCategory();
        return JsonResult.ok(categoryListItemVOPageData);
    }
    /**
     * 把PageData-list数据里的BO 转换为 CategoryListItemVO
     * @param pageData
     * @return
     */
    private PageData<CategoryListItemVO> getCategoryBOPageData(PageData<CategoryBO> pageData) {
        PageData<CategoryListItemVO> result = new PageData<>();
        List<CategoryBO> list = pageData.getList();
        List<CategoryListItemVO> categoryBOList = new ArrayList<>();
        for (CategoryBO categoryBO : list) {
            CategoryListItemVO categoryListItemVO = new CategoryListItemVO();
            BeanUtils.copyProperties(categoryBO,categoryListItemVO);
            categoryBOList.add(categoryListItemVO);
        }
        BeanUtils.copyProperties(pageData,result);
        result.setList(categoryBOList);
        return result;
    }




    @ApiOperationSupport(order = 100)
    @ApiOperation("添加类别")
    @PostMapping("/category/add")
    public JsonResult add(@Valid CategoryAddParam categoryAddParam){
        //打印日志的目的是为了出现问题好排查  问题 排查
        // 1都会出现什么问题?
        //    接口没找到
        //    插入的分类已经存在?
        //       1 前端的入参有问题
        //       2 我们的代码逻辑有问题;
        log.debug("开始处理添加分类请求,入参:{}", categoryAddParam);
        categoryService.addCategory(categoryAddParam);
        JsonResult jsonResult = JsonResult.ok();
        log.debug("时间 处理添加分类请求完成,入参:{},出参:{}", categoryAddParam,jsonResult);
        return jsonResult;
        //前端老哥 有点疏忽, if state == 200 ,保存失败
    }

    @ApiOperationSupport(order = 300)
    @ApiOperation("查询类别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "类别id",required = true,dataType = "Long")
    })
    @GetMapping("/category/get/{id}")
    public JsonResult getCategoryById(@PathVariable Long id){
        return null;
    }

    @ApiOperationSupport(order = 400)
    @ApiOperation("修改类别")
    @PostMapping("/category/update")
    public JsonResult updateCategoryById(CategoryUpdateParam categoryUpdateParam){
        return null;
    }

    @ApiOperationSupport(order = 500)
    @ApiOperation("删除类别")
    @PostMapping("/category/del/{id}")
    public JsonResult deleteCategoryById(@PathVariable Long id){
        return null;
    }
}
